package operation;

import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.NameMatcher;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IFormMain;
import com.supermap.desktop.core.implement.SmRibbonButton;
import org.pushingpixels.flamingo.api.ribbon.JRibbon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
import robotCross.TextMatcher;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Field;

/**
 * Created by caolp on 2017-11-07.
 */
public class RibbonOperation extends Operation {
	public static RibbonOperation RIBBONOPERATION;
	SmRibbonButton dropDownButton = null;

	static {
		RIBBONOPERATION = new RibbonOperation();
		IFormMain mainFrame = Application.getActiveApplication().getMainFrame();
		if (mainFrame instanceof JRibbonFrame) {
			JRibbon ribbon = ((JRibbonFrame) mainFrame).getRibbon();
			RIBBONOPERATION.setContainer(ribbon);
		}
	}

	private RibbonOperation() {
	}


	/**
	 * 点击Ribbon上的按钮
	 * @param toggleName
	 * @param buttonName
	 * @return
	 */

	public void clickButton(String toggleName, String buttonName) throws ComponentNotFoundException, MultipleComponentsFoundException {
		Component component = searchRibbon(toggleName);
		super.clickComponent(component);
		super.clickComponent(new NameMatcher(buttonName));
	}

	/**
	 * 点击Ribbon上的下拉按钮
	 * @param toggleName
	 * @param dropDownName
	 * @param menuName
	 * @return
	 */
	public void clickDropDownButton(String toggleName, String dropDownName, String menuName) throws ComponentNotFoundException, MultipleComponentsFoundException {
		Component component = searchRibbon(toggleName);
		super.clickComponent(component);
		dropDownButton = (SmRibbonButton) super.clickComponent(new NameMatcher(dropDownName));
		clickDropdownMenu(menuName);
	}


	public void clickDropdownMenu(String menuPath) throws ComponentNotFoundException, MultipleComponentsFoundException {
		Field field = null;
		Component popup = null;
		try {
			field = dropDownButton.getClass().getDeclaredField("menu");
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		field.setAccessible(true);
		try {
			popup = (Component) field.get(dropDownButton);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		new Operation((Container) popup).clickComponent(new TextMatcher(menuPath));
	}


	/**
	 * 查找Ribbon，如开始、地图、空间分析等最上层分组，避免因控件名称重复，而导致查找错误等问题。
	 *
	 * @param toggleName
	 * @return
	 */
	public Component searchRibbon(String toggleName) {
		Component component = null;
		try {
			JRibbon ribbon = (JRibbon) container;
			JPanel panel = (JPanel) ((JPanel) ribbon.getComponent(2)).getComponent(0);
			JPanel panelToggle = ((JPanel) panel.getComponent(0));
			for (int i = 0; i < panelToggle.getComponents().length; i++) {
				if (panelToggle.getComponent(i).getName().equalsIgnoreCase(toggleName)) {
					component = panelToggle.getComponent(i);
					return component;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return component;
	}
}
